package com.tangyee.home.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.tangyee.MainActivity;
import com.tangyee.R;
import com.tangyee.home.activity.db.UsersInfo;
import com.tangyee.utils.SessionHelper;
import com.tangyee.utils.SessionReceiver;
import com.tangyee.utils.SessionService;
import com.tangyee.utils.SessionUtil;

import org.litepal.tablemanager.Connector;

public class Register extends AppCompatActivity implements View.OnClickListener, SessionHelper {
    private Button register, get_code;
    private ImageView back;
    //手机号码，密码，验证码
    public EditText phone, password, code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        init();
        registerSessionReceiver();
    }

    public void init() {
        register = (Button) findViewById(R.id.btn_register);
        register.setOnClickListener(this);
        back = (ImageView) findViewById(R.id.reg_back);
        back.setOnClickListener(this);
        password = (EditText) findViewById(R.id.et_password);
        phone = (EditText) findViewById(R.id.et_phone_num);
        get_code = (Button) findViewById(R.id.bn_sms_code);
        code = (EditText) findViewById(R.id.et_sms_code);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register: {
                register();
            }
            break;
            case R.id.reg_back: {
                Intent intent = new Intent(this, Login.class);
                startActivity(intent);
                finish();
            }
            break;
        }
    }

    public void register() {
        Connector.getDatabase();//创建数据库
        String phonenumber = phone.getText().toString().trim();
        String pwd = password.getText().toString().trim();
        String defaultUsername = phonenumber;
        //判断密码
        if (phonenumber.length() > 0 && pwd.length() > 0 && SessionUtil.isPhonenumber(phonenumber)) {
            //注册成功才能保存
//            UsersInfo users = new UsersInfo();
//            users.setUsername(phone.getText().toString().trim());
//            users.setPassword(password.getText().toString().trim());
//            users.setStatus(1);
//            users.save();
            //这是我想注册通过Service
//            Intent intent = new Intent(I_WANT_REGISTER_BY_SERVICE);//我想注册
//            intent.setPackage(getPackageName());
//            intent.putExtra(USERNAME,defaultUsername);
//            intent.putExtra(PASSWORD,pwd);
//            intent.putExtra(PHONENUMBER,phonenumber);
//            startService(intent);
            //这是我想注册通过Broad
            Intent intent = new Intent(this, SessionReceiver.class);
            intent.setAction(I_WANT_REGISTER_BY_BROAD);
            intent.putExtra(USERNAME, defaultUsername);
            intent.putExtra(PASSWORD, pwd);
            intent.putExtra(PHONENUMBER, phonenumber);
            sendBroadcast(intent);
        } else {
            Toast.makeText(this, "手机号码好像出错了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void registerSessionReceiver() {
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                sessionid = intent.getStringExtra(SESSIONID);
                jsonStr = intent.getStringExtra(JSON);
                Log.d(TAG, "jsonStr是" + jsonStr);
                localHander(context, intent);
                Log.d(TAG, "Hllo,tangyee");
                Log.d(TAG, "接受的用户名:" + intent.getStringExtra(USERNAME));
                Log.d(TAG, "接受的密码是：" + intent.getStringExtra(PASSWORD));
            }
        };
        IntentFilter filter = new IntentFilter(GET_A_SESSIONID);//接收的广播信号是GAVE_A_SESSION
        registerReceiver(receiver, filter);
    }

    @Override
    public void unregisterSessionReceiver() {
        unregisterReceiver(receiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterSessionReceiver();
    }

    public void loginFast(String jsonString) {
        Log.d(TAG, "JsonStr 是 " + jsonString);
        Intent intent = new Intent(this, MainActivity.class);
        //新建一个栈来存放被启动的activity.
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(SESSIONID, sessionid);
        intent.putExtra(JSON, jsonString);
        intent.putExtra("preActivity", "Register.class");
        startActivity(intent);
        finish();
    }
    public void localHander(Context context, Intent intent) {
        Log.d(TAG, "状态码为:" + intent.getIntExtra(STATE, -1));
        int state = intent.getIntExtra(STATE, -1);
        if (SessionUtil.isSuccessful(state)) {
            UsersInfo usersInfo = new UsersInfo();
            usersInfo.setUsername(intent.getStringExtra(USERNAME));
            usersInfo.setStatus(1);
            usersInfo.setSessionid(intent.getStringExtra(SESSIONID));
            usersInfo.setPassword(intent.getStringExtra(PASSWORD));
            usersInfo.save();
            loginFast(jsonStr);
        } else {
            Toast.makeText(this, SessionUtil.getRegisterHint(state), Toast.LENGTH_SHORT).show();
        }
    }

    private static final String TAG = "Register";
    private BroadcastReceiver receiver;
    private String sessionid;
    private String jsonStr;
}
